<?php

require_once 'UC_DBBase.php';
require_once 'UC_DataType.php';

class UC_Column extends UC_DBBase
{
    public function __construct()
    {
        
    }
    
    public function Associators_()
    {
        
    }
    
    public function Clone_()
    {
        
    }
    
    public function CompareTo_()
    {
        
    }
    
    public function Delete_()
    {
        
    }
    
    public function ExecuteMethod_()
    {
        
    }
    
    static function Exists_(UC_DBObjectPath $object_path)
    {
        if($object_path->get_is_Column())
        {
            $sql = "Select `COLUMN_NAME` From `INFORMATION_SCHEMA`.`COLUMNS` " .
                "Where `TABLE_SCHEMA` = '" . $object_path->get_Schema() . "' " .
                "And `TABLE_NAME` = '" . $object_path->get_Table() . "' " . 
                "And `COLUMN_NAME` = '" . $object_path->get_Name() . "'";
            
            try
            {
                if($object_path->get_Server()->get_PDO()->query($sql)->rowCount() == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (PDOException $e)
            {
                throw new UC_DBException(
                    'Unable to determine if the column exists.');
            }
        }
        else
        {
            return false;
        }
    }
    
    static function Get_(UC_DBObjectPath $object_path)
    {
        if($object_path->get_is_Column())
        {
            if(self::Exists_($object_path))
            {
                $column = new UC_Column();
                $column->set_Path_($object_path);
                return $column;
            }
            else
            {
                throw new InvalidArgumentException(
                    'Invalid object path.');
            }
        }
        else
        {
            throw new InvalidArgumentException(
                'Invalid object path.');
        }
    }
    
    static function Instances_()
    {
        
    }
    
    public function Put_()
    {
        
    }
    
    public function References_()
    {
        
    }
    
    static function SpawnInstance_()
    {
        
    }
    
    public function get_Name()
    {
        return $this->get_Path_()->get_Name();
    }
    
    public function set_Name($Name)
    {
        if(is_string($Name))
        {
            $this->get_Path_()->set_Name($Name);
        }
        else
        {
            throw new InvalidArgumentException(
                'The name should be a string.');
        }
    }
    
    public function get_AutoIncrement()
    {
        return $this->AutoIncrement;
    }

    public function set_AutoIncrement($AutoIncrement)
    {
        if(is_bool($AutoIncrement))
        {
            $this->AutoIncrement = $AutoIncrement;
        }
        else
        {
            throw new InvalidArgumentException(
                'The value should be boolean.');
        }
    }

    public function get_CharacterMaximumLength()
    {
        return $this->CharacterMaximumLength;
    }

    public function set_CharacterMaximumLength($CharacterMaximumLength)
    {
        $this->CharacterMaximumLength = $CharacterMaximumLength;
    }

    public function get_CharacterSet()
    {
        return $this->CharacterSet;
    }

    public function set_CharacterSet($CharacterSet)
    {
        $this->CharacterSet = $CharacterSet;
    }

    public function get_Collation()
    {
        return $this->Collation;
    }

    public function set_Collation($Collation)
    {
        $this->Collation = $Collation;
    }

    public function get_DataType()
    {
        return $this->DataType;
    }

    public function set_DataType($DataType)
    {
        if(UC_DataType::Defines($DataType))
        {
            $this->DataType = $DataType;
        }
        else
        {
            throw new InvalidArgumentException(
                'The value should be one of the UC_DataType constants.');
        }
    }

    public function get_Default()
    {
        return $this->Default;
    }

    public function set_Default($Default)
    {
        $this->Default = $Default;
    }

    public function get_InPrimaryKey()
    {
        if(isset($this->InPrimaryKey))
        {
            return $this->InPrimaryKey;
        }
        else
        {
            $sql = "Select `COLUMN_KEY` From `INFORMATION_SCHEMA`.`COLUMNS` " .
                "Where `TABLE_SCHEMA` = '" . $this->get_Path_()->get_Schema() . "' " .
                "And `TABLE_NAME` = '" . $this->get_Path_()->get_Table() . "' " . 
                "And `COLUMN_NAME` = '" . $this->get_Path_()->get_Name() . "'";
            
            foreach($this->get_Path_()->get_Server()->get_PDO()->query($sql) as $row)
            {
                if($row['COLUMN_KEY'] == 'PRI')
                {
                    $this->set_InPrimaryKey(true);
                    return true;
                }
            }
            $this->set_InPrimaryKey(false);
            return false;
        }
    }

    public function set_InPrimaryKey($InPrimaryKey)
    {
        if(is_bool($InPrimaryKey))
        {
            $this->InPrimaryKey = $InPrimaryKey;
        }
        else 
        {
            throw new InvalidArgumentException
                ('The value should be boolean.');
        }
    }

    public function get_IsForeignKey()
    {
        return $this->IsForeignKey;
    }

    public function set_IsForeignKey($IsForeignKey)
    {
        if(is_bool($IsForeignKey))
        {
            $this->IsForeignKey = $IsForeignKey;
        }
        else
        {
            throw new InvalidArgumentException(
                'The value should be boolean.');
        }
    }

    public function get_Nullable()
    {
        return $this->Nullable;
    }

    public function set_Nullable($Nullable)
    {
        if(is_bool($Nullable))
        {
            $this->Nullable = $Nullable;
        }
        else
        {
            throw new InvalidArgumentException(
                'The value should be boolean.');
        }
    }
    
    public function get_OrdinalPosition()
    {
        return $this->OrdinalPosition;
    }

    public function set_OrdinalPosition($OrdinalPosition)
    {
        $this->OrdinalPosition = $OrdinalPosition;
    }

    public function get_Precision()
    {
        return $this->Precision;
    }

    public function set_Precision($Precision)
    {
        $this->Precision = $Precision;
    }

    public function get_Scale()
    {
        return $this->Scale;
    }

    public function set_Scale($Scale)
    {
        $this->Scale = $Scale;
    }

    private $AutoIncrement;
    private $CharacterMaximumLength;
    private $CharacterSet;
    private $Collation;
    private $DataType;
    private $Default;
    private $InPrimaryKey;
    private $IsForeignKey;
    private $OrdinalPosition;
    private $Precision;
    private $Scale;
    private $Nullable;
    
}
?>
